﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebLibSolPedBE;

namespace WebApplication1
{
    public partial class index : System.Web.UI.Page
    {
        List<SolPedBE> lst = null;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Session["nombreApp"] = "libsolped";
                if (Session["objUser"] == null)
                {
                    Response.Redirect("credencial.aspx");
                }
                else
                {
                    ModalPopupExtender1.Hide();
                    //Give the data to the grid
                    lst = LibSolPedDALF.DaoFactory.LibSolPedF.getLstSolPed();
                    gvGrid.DataSource = lst;
                    gvGrid.DataBind();

                    Session.Add("lstSolped", lst);
                    Session.Add("accion", "");
                }
            }
            else
            {
                string acci = Session["accion"] as string;
                if (acci.Equals("R"))
                {
                    lst = LibSolPedDALF.DaoFactory.LibSolPedF.getLstSolPed();
                    gvGrid.DataSource = lst;
                    gvGrid.DataBind();
                    Session.Add("lstSolped", lst);
                    Session.Add("accion", "");
                }
            }
        }

        protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find the control
                var lblPREQNO_BNFPO_CENTRO = (Label)e.Row.FindControl("lblPREQNO_BNFPO_CENTRO");
                var lblDescMat = (Label)e.Row.FindControl("lblDescMat");
                var lblCreateBy = (Label)e.Row.FindControl("lblCreateBy");
                var lblCentroCost = (Label)e.Row.FindControl("lblCentroCost");
                //Display the data
                var data = e.Row.DataItem;
                SolPedBE it = (SolPedBE)data;
                lblPREQNO_BNFPO_CENTRO.Text = it.PREQ_NO + "-" + it.BNFPO + "-" + it.CENTRO;// "11001247 - 00020 - 1101";
                lblDescMat.Text = it.DESCMAT;
                lblCreateBy.Text = it.CREATED_BY;
                lblCentroCost.Text = it.CENTROCOSTO;

            }
        }
        protected void gvGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView gv = (GridView)sender;
            int filaSeleccionada = gv.SelectedIndex;
            lst = (List<SolPedBE>)Session["lstSolped"];
            //lst[filaSeleccionada].BNFPO;
            try
            {
                Session.Add("objSolPed", lst[filaSeleccionada]);
                Response.Redirect("detalle.aspx");
            }
            catch (Exception ex)
            {
                
            }
        }
    }
}